//
//  SYTopLabView.m
//  SanYiTeaching
//
//  Created by 郑浩然 on 2019/4/22.
//  Copyright © 2019年 SanYi. All rights reserved.
//

#import "SYTopLabView.h"

@interface SYTopLabView ()
@property (strong, nonatomic) NSArray *nameArr;
@property (assign, nonatomic) CGFloat W;
@end


@implementation SYTopLabView

- (instancetype)initWithDataNameArr:(NSArray *)dataNameArr width:(CGFloat)width{
    self = [super init];
    if (self) {
        self.nameArr = dataNameArr;
        self.W = width;
        [self initSubView];
    }
    return self;
}

- (void)initSubView {
    [self addSubview:self.jinXingView];
    [self.jinXingView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.bottom.left.right.equalTo(self);
    }];
    
    UILabel *contLab = nil;
    NSMutableArray *titleArr = [NSMutableArray array];
    for (NSInteger i = 0; i < _nameArr.count; i++) {
        UILabel *title = [UILabel titleColour:TITLECOLOR
                                   titleFount:13
                                     pingfang:@"PingFang-SC-Medium"
                                    alignment:NSTextAlignmentCenter
                                    titleLine:1];
        title.text = _nameArr[i];
        [self.jinXingView addSubview:title];
        [titleArr addObject:title];
        
        if (i == 0) {
            contLab = title;
            [title mas_makeConstraints:^(MASConstraintMaker *make) {
                make.left.equalTo(self.jinXingView.mas_left);
                make.top.equalTo(self.jinXingView.mas_top);
                make.width.equalTo(self.jinXingView).multipliedBy(self.W);
                make.height.mas_equalTo(40);
            }];
        } else {
            [title mas_makeConstraints:^(MASConstraintMaker *make) {
                make.left.equalTo(contLab.mas_right);
                make.top.equalTo(self.jinXingView.mas_top);
                make.width.equalTo(self).multipliedBy(self.W);
                make.height.mas_equalTo(40);
            }];
            contLab = title;
            if (i == _nameArr.count-1) {
                title.userInteractionEnabled = YES;
                [title addSubview:self.paiMingChangeButton];
                [_paiMingChangeButton mas_makeConstraints:^(MASConstraintMaker *make) {
                    make.right.equalTo(@0).offset(-16);
                    make.centerY.equalTo(title.mas_centerY);
                    make.width.equalTo(@14);
                    make.height.equalTo(@12);
                }];
            }
        }
    }
}

- (UIButton *)paiMingChangeButton{
    if (!_paiMingChangeButton) {
        UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
        [button setBackgroundImage:[UIImage imageNamed:@"arrow_down"] forState:UIControlStateNormal];
        [button setBackgroundImage:[UIImage imageNamed:@"arrow_up"] forState:UIControlStateSelected];
        button.hidden = YES;
        _paiMingChangeButton = button;
    }
    return _paiMingChangeButton;
}

- (UIView *)jinXingView {
    if (!_jinXingView) {
        _jinXingView = [[UIView alloc] init];
        _jinXingView.backgroundColor = [UIColor colorWithHexString:@"#F8F8F8"];
    }
    return _jinXingView;
}
- (void)drawRect:(CGRect)rect{
    
}
@end
